package cn.mesmile.activity.listener1;

import cn.hutool.extra.spring.SpringUtil;
import cn.mesmile.activity.util.CustomSpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2022/1/4 10:43
 * @Description
 */
@Slf4j
@Component
public class ActivityEventGlobalListener implements ActivitiEventListener {

    @Override
    public void onEvent(ActivitiEvent event) {
        // 所有的事件都会进入到这里
//        System.out.println("----------全局监听器-----------");
        ActivitiEventType type = event.getType();
//        System.out.println("type.name() = " + type.name());

        if (ActivitiEventType.PROCESS_CANCELLED.equals(type)){
            System.out.println(">>>>>>>>> runtimeService终止流程 >>>>>>>>>");
            TaskService taskService = CustomSpringUtils.getBean(TaskService.class);
            // 查询代办任务
            List<Task> tasks = taskService.createTaskQuery().processInstanceId(event.getProcessInstanceId()).list();
            for (Task task : tasks) {
                // 待审批人
                String assignee = task.getAssignee();
                log.info(">>>>>>>>>>> 当前审批人 : {} ,任务id：{}", assignee, task.getId());
                // 通知待审批人，流程终止了
            }

            System.out.println("type.name() = " + type.name());
            System.out.println(">>>>>>>>> runtimeService终止流程 >>>>>>>>>");
        }

    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

}
